package com.sense.kukoo.biz_library.adapter.provider

import android.text.Html
import android.view.View
import com.sense.kukoo.biz_library.adapter.SubscriptionsAdapter
import com.sense.kukoo.common.R
import com.sense.kukoo.common.media.bean.LocalMediaListItemBean
import com.sense.kukoo.common.media.watcher.UserWatcher
import com.sense.kukoo.common.repo.bean.BookItemBean
import com.sense.kukoo.common.repo.bean.SubscriptionsBean
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.common.wiget.SubscriptionsItemView
import com.sense.kukoo.common.wiget.adapter.provider.MultiItemProvider
import com.sense.kukoo.common.wiget.adapter.viewholder.BaseViewHolder
import com.sense.kukoo.lib_picture.config.GlideConfig
import com.sense.kukoo.lib_picture.loader.Monet

class RecentlyUpdatedItemProvider(val listener: SubscriptionsAdapter.SubscriptionsEventListener) :
    MultiItemProvider<SubscriptionsBean>() {

    private val TAG: String = javaClass.simpleName

    private var mCurrentMusic: LocalMediaListItemBean? = null
    private var mCurrentState: Int = UserWatcher.MusicStateWatcher.STATE_NOT_PLAYING

    override fun getItemType(): Int = SubscriptionsBean.TYPE_RECENTLY_UPDATED_ITEM

    override fun getItemLayoutId(): Int = R.layout.common_subscriptions_item_container

    override fun onBindViewHolder(viewHolder: BaseViewHolder, item: SubscriptionsBean) {
        val view = viewHolder.getView(R.id.item) as SubscriptionsItemView
        view.setItemClickListener {
            listener.onRecentlyUpdatedItemClick(item)
        }
        view.setPlayListener {
            listener.onRecentlyUpdatedPlay(item)
        }
        //
        val bean: BookItemBean = item.recentItem!!
        view.setIconUrl(bean.coverUrl)
        view.setType(bean.name)
        view.setDes(Html.fromHtml(bean.introduction ?: ""))

        //设置相对时间
        val time = System.currentTimeMillis() - (bean.createdAtTimestamp ?: 0)
        val sec :Long = time / 1000
        val min = sec / 60
        val hour = min / 60
        val day = hour / 24
        val week = day / 7
        val month = day / 30
        val year = day / 365
        if(sec < 60){
            view.setTimeLeft(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.subscriptions_updated_just_now))
        }else if(min < 60){
            view.setTimeLeft(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.subscriptions_updated_min, min))
        }else if(hour < 24){
            view.setTimeLeft(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.subscriptions_updated_hour, hour))
        }else if(day < 7){
            view.setTimeLeft(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.subscriptions_updated_day, day))
        }else if(day < 31){
            view.setTimeLeft(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.subscriptions_updated_week, week))
        }else if(day < 365){
            view.setTimeLeft(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.subscriptions_updated_month, month))
        }else {
            view.setTimeLeft(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.subscriptions_updated_year, year))
        }
        //
        val timeTotal = calculateTime(bean.duration ?: 0)
        view.setTimeTotal(timeTotal)
        //
        if(mCurrentMusic != null && mCurrentMusic?.idOfBooks() == bean.idOfBook() && mCurrentMusic?.idOfChapter() == bean.idOfChapter()){
            //当前音乐
            when (mCurrentState) {
                UserWatcher.MusicStateWatcher.STATE_PREPARE,
                UserWatcher.MusicStateWatcher.STATE_ERROR_RETRYING,
                UserWatcher.MusicStateWatcher.STATE_SEEKING,
                UserWatcher.MusicStateWatcher.STATE_PLAYING -> {
                    //播放中
                    view.setBtnIsPlayOrPause(false)
                }
                else -> {
                    //暂停，其它状态
                    view.setBtnIsPlayOrPause(true)
                }
            }
        } else {
            //非当前
            view.setBtnIsPlayOrPause(true)
        }
    }

    private fun calculateTime(time: Int): String {
        return if (time >= 60) {
            val minute = time / 60
            val second = time % 60
            (if (minute < 10) "0$minute" else "" + minute) + if (second < 10) ":0$second" else ":$second"
        } else {
            if (time < 10) "00:0$time" else "00:$time"
        }
    }

    fun setCurrentMusic(music: LocalMediaListItemBean){
        mCurrentMusic = music
    }

    fun setCurrentPlayState(state: Int){
        mCurrentState = state
    }
}